package com.ubing.boot.notesforjava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ubing.boot.notesforjava.dao.NoteInfoMapper;
import com.ubing.boot.notesforjava.dao.TzlMapper;
import com.ubing.boot.notesforjava.domel.entity.NoteInfo;
import com.ubing.boot.notesforjava.domel.entity.Tzl;
import com.ubing.boot.notesforjava.domel.entity.UserInfo;
import com.ubing.boot.notesforjava.response.Response;
import com.ubing.boot.notesforjava.service.INoteInfoService;
import com.ubing.boot.notesforjava.utils.IdGeneratorSnowflake;
import com.ubing.boot.notesforjava.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ubing
 * @since 2023-02-20
 */
@Service
public class INoteInfoServiceImpl extends ServiceImpl<NoteInfoMapper, NoteInfo> implements INoteInfoService {

    @Autowired
    NoteInfoMapper noteInfoMapper;
    @Autowired
    IdGeneratorSnowflake snowflake;
    @Override
    public Response insert(NoteInfo noteInfo) {
        noteInfo.setCreateTime(LocalDateTime.now());
        noteInfo.setUpdateTime(LocalDateTime.now());
        noteInfo.setNoteId(snowflake.snowflakeId());
        int insert = noteInfoMapper.insert(noteInfo);
        return insert==0?Response.failure("新增失败"):Response.success();
    }

    @Override
    public Response detail(String noteId) {
        NoteInfo noteInfo = noteInfoMapper.selectById(noteId);
        return noteInfo==null?Response.failure("查询失败"):Response.success(noteInfo);
    }

    @Resource
    TzlMapper tzlMapper;
    @Override
    public Response getList(Integer pageNo, Integer pageSize) {
//        Tzl tzl = new Tzl();
//        tzl.setName("tzl");
//        Integer inserted = tzlMapper.insertAndReturnId(tzl);
//        System.out.println(tzl.getId());
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName("ubing");

        ThreadLocalUtils.set(userInfo);
        System.out.println(Thread.currentThread().getName());


        List<NoteInfo> list = noteInfoMapper.getList(pageNo, pageSize);
        return Response.success(list);
    }

    @Override
    public Response getListById(Integer pageNo,Integer pageSize,NoteInfo noteInfo) {
        List<NoteInfo> noteInfos = noteInfoMapper.getListById(pageNo,pageSize,noteInfo.getUserId());
        return Response.success(noteInfos);
    }

    @Override
    public Response excel(List prjNm) {
        return Response.success(baseMapper.queryIds(prjNm));
    }


}
